package pro.common.io;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import pro.entities.XmlEntity;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class XmlCmd  extends  BaseCmd{

    public XmlCmd(String basepath) {
        super(basepath);
    }

    /**
     * @param fileName
     * @return
     */
    @Override
    public Object read(String fileName) {
        return null;
    }

    /**
     * @param fileName
     * @param obj
     * @return
     */
    @Override
    public Boolean write(String fileName, Object obj){
        XmlEntity xml = (XmlEntity)obj;

        Document document = DocumentHelper.createDocument();

        // add and get the root element
        Element root = document.addElement(xml.rootName);

        // 为根元素添加子元素
        Element node = root.addElement(xml.node);


        // 为子元素添加属性
        Element stock = node.addElement(xml.stock.stockName);

        // 为子元素添加子元素
        stock.addAttribute("exchangeName", xml.stock.exchangeName);


        //Write into a document
        Writer writer = null;
        try {
            String filePath = new File( this.dicFullPath, fileName).getPath();

            writer = new FileWriter(new File(filePath));

            document.write(writer);

            writer.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        return false;
    }
}
